/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.Fv;
import java.awt.image.BufferedImage;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.TankTools;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.fv")
public class FvFacadeREST extends AbstractFacade<Fv> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public FvFacadeREST() {
        super(Fv.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public Fv create(@WebParam(name = "fv") Fv entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Fv entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Fv find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Fv> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Fv> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Fv> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }
    
    @GET
    @Path("{id}/tankStatusImg")
    @Produces({"image/png"})
    public BufferedImage findFooterImg(@PathParam("id") Integer id) {
        Fv vessel = super.find(id);
        TankTools tt = new TankTools();
        if(vessel.getFvprm()){
            //if the vessel is a primary fermantation vessel
            for(final Brews brew : getBrewItems(em)){
                if((brew.getBState()==5)&&(brew.getPfvid()==vessel.getFvid())){
                    return tt.createIcon("fermTank.png", brew, vessel);
                } else if((brew.getBState()==10)&&(brew.getSfvid()==vessel.getFvid())){
                    return tt.createIcon("fermTank.png", brew, vessel);
                } else if((brew.getBState()==15)&&(brew.getCvid()==vessel.getFvid())){
                    return tt.createIcon("fermTank.png", brew, vessel);
                } 
            }
            return tt.createIcon("fermTank.png", null, vessel);
        } else {
            for(final Brews brew : getBrewItems(em)){
                if((brew.getBState()==5)&&(brew.getPfvid()==vessel.getFvid())){
                    return tt.createIcon("brtTank.png", brew, vessel);
                } else if((brew.getBState()==10)&&(brew.getSfvid()==vessel.getFvid())){
                    return tt.createIcon("brtTank.png", brew, vessel);
                } else if((brew.getBState()==15)&&(brew.getCvid()==vessel.getFvid())){
                    return tt.createIcon("brtTank.png", brew, vessel);
                }
            } 
            return tt.createIcon("brtTank.png", null, vessel);
        }
    }
    
    private List<Brews> getBrewItems(final EntityManager en){
        final String str = "SELECT b FROM Brews b WHERE b.bState<'20' OR b.bState='25'";
        TypedQuery<Brews> query = en.createQuery(str, Brews.class);
        List<Brews> brewItems = query.getResultList();
        return brewItems;
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
